package com.jinzhi.jzcultivate.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.jzapi.training.pojo.CoursesAndTeacherVo;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@AllArgsConstructor
@RequestMapping("/cultivate/study")
public class StudyController extends BaseController {

	private CoursesService coursesService;
    @Autowired
    private ChapterItemBankService chapterItemBankService;
    @Autowired
    private TrainingResourcesService trainingResourcesService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private ClassesExaminationSubmitService classesExaminationSubmitService;
    @Autowired
    private ClassesExaminationPaperService classesExaminationPaperService;


	@Log("在线学习")
	@GetMapping("/studyOnline")
	String studyOnline(Model model){
		return "cultivateWeb/study/studyOnline";
	}

	@Log("学习详情")
	@GetMapping("/studyInfo")
	String studyInfo(Model model, String id, String isTestScores, String classId, String taskId){
		CoursesAndTeacherVo coursesAndTeacherVo = coursesService.selCourseDetail(Long.parseLong(id));
		model.addAttribute("cou" , coursesAndTeacherVo);
		model.addAttribute("isTestScores",isTestScores);
		if(StringUtils.isNotBlank(classId)) {
            model.addAttribute("classId", classId);
        }else {
            model.addAttribute("classId", "");
        }
        if(StringUtils.isNotBlank(taskId)) {
            model.addAttribute("taskId", taskId);
        }else {
            model.addAttribute("taskId", "");
        }

        List<Map> mapList = new ArrayList<>();
        if (StringUtils.isNotBlank(id)) {
            model.addAttribute("coursesId", Long.parseLong(id));
            List<ChapterDO> chapterDOS = chapterService.findByKv("courses_id", Long.parseLong(id), "is_root", 1);
            if (chapterDOS != null && chapterDOS.size() > 0) {
                for (ChapterDO chapterDO : chapterDOS) {
                    Map map1 = new HashMap();
                    map1.put("id", chapterDO.getId());
                    List<ChapterItemBankDO> chapterItemBankDOS = chapterItemBankService.findByKv("chapter_id", chapterDO.getId(), "type", 0);
                    if (chapterItemBankDOS != null && chapterItemBankDOS.size() > 0) {
                        map1.put("isChaItem", "true");
                    } else {
                        map1.put("isChaItem", "false");
                    }
                    if (chapterDO.getName() != null) {
                        map1.put("name", chapterDO.getName());
                    } else {
                        map1.put("name", "");
                    }
                    map1.put("isFree", chapterDO.getIsFree());//是否免费

                    map1.put("level", chapterDO.getLevel());//层级

                    List<TrainingResourcesDO> trainingResourcesDOS = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO.getId());//,"type",1
                    if (trainingResourcesDOS != null && trainingResourcesDOS.size() > 0) {
                        int i = 0;
                        for (TrainingResourcesDO r : trainingResourcesDOS) {
                            r.setRindex(i);
                            i++;
                        }
                    }
                    map1.put("resources", trainingResourcesDOS);
                    if (trainingResourcesDOS.size() > 0) {
                        map1.put("isHaveResources", 1);
                    } else {
                        map1.put("isHaveResources", 0);
                    }

                    List<Map> map1Children = new ArrayList<>();
                    List<ChapterDO> chapterDOS2 = chapterService.findByKv("courses_id", Long.parseLong(id), "parent_id", chapterDO.getId());
                    if (chapterDOS2 != null && chapterDOS2.size() > 0) {
                        for (ChapterDO chapterDO2 : chapterDOS2) {
                            Map map2 = new HashMap();
                            map2.put("id", chapterDO2.getId());
                            List<ChapterItemBankDO> chapterItemBankDOS2 = chapterItemBankService.findByKv("chapter_id", chapterDO2.getId(), "type", 0);
                            if (chapterItemBankDOS2 != null && chapterItemBankDOS2.size() > 0) {
                                map2.put("isChaItem", "true");
                            } else {
                                map2.put("isChaItem", "false");
                            }
                            if (chapterDO2.getName() != null) {
                                map2.put("name", chapterDO2.getName());
                            } else {
                                map2.put("name", "");
                            }
                            map2.put("isFree", chapterDO2.getIsFree());//是否免费

                            map2.put("level", chapterDO2.getLevel());//层级

                            List<TrainingResourcesDO> trainingResourcesDOS2 = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO2.getId());//,"type",1
                            if (trainingResourcesDOS2 != null && trainingResourcesDOS2.size() > 0) {
                                int i = 0;
                                for (TrainingResourcesDO r : trainingResourcesDOS2) {
                                    r.setRindex(i);
                                    i++;
                                }
                            }
                            map2.put("resources", trainingResourcesDOS2);
                            if (trainingResourcesDOS2.size() > 0) {
                                map2.put("isHaveResources", 1);
                            } else {
                                map2.put("isHaveResources", 0);
                            }

                            List<Map> map2Children = new ArrayList<>();
                            List<ChapterDO> chapterDOS3 = chapterService.findByKv("courses_id", Long.parseLong(id), "parent_id", chapterDO2.getId());
                            if (chapterDOS3 != null && chapterDOS3.size() > 0) {
                                for (ChapterDO chapterDO3 : chapterDOS3) {
                                    System.out.println("id:" + chapterDO3.getId() + " -- name:" + chapterDO.getName());
                                    Map map3 = new HashMap();
                                    map3.put("id", chapterDO3.getId());
                                    List<ChapterItemBankDO> chapterItemBankDOS3 = chapterItemBankService.findByKv("chapter_id", chapterDO3.getId(), "type", 0);
                                    if (chapterItemBankDOS3 != null && chapterItemBankDOS3.size() > 0) {
                                        map3.put("isChaItem", "true");
                                    } else {
                                        map3.put("isChaItem", "false");
                                    }
                                    if (chapterDO3.getName() != null) {
                                        map3.put("name", chapterDO3.getName());
                                    } else {
                                        map3.put("name", "");
                                    }
                                    map3.put("isFree", chapterDO3.getIsFree());//是否免费

                                    map3.put("level", chapterDO3.getLevel());//层级

                                    List<TrainingResourcesDO> trainingResourcesDOS3 = trainingResourcesService.findByKv("courses_id", Long.parseLong(id), "chapter_id", chapterDO3.getId());//,"type",1
                                    if (trainingResourcesDOS3 != null && trainingResourcesDOS3.size() > 0) {
                                        int i = 0;
                                        for (TrainingResourcesDO r : trainingResourcesDOS3) {
                                            r.setRindex(i);
                                            i++;
                                        }
                                    }
                                    map3.put("resources", trainingResourcesDOS3);

                                    if (trainingResourcesDOS3.size() > 0) {
                                        map3.put("isHaveResources", 1);
                                    } else {
                                        map3.put("isHaveResources", 0);
                                    }
                                    map2Children.add(map3);
                                }
                                map2.put("isHaveChildren", 1);
                                map2.put("children", map2Children);
                            } else {
                                map2.put("isHaveChildren", 0);
                                map2.put("children", "");
                            }
                            map1Children.add(map2);
                        }
                        map1.put("isHaveChildren", 1);
                        map1.put("children", map1Children);
                    } else {
                        map1.put("isHaveChildren", 0);
                        map1.put("children", "");
                    }
                    mapList.add(map1);
                }
            }
        }
        // System.out.println("mapList:"+mapList.toString());
        model.addAttribute("selChapterList", mapList);

		return "cultivateWeb/study/studyInfo";
	}

	@Log("答题解析列表")
	@GetMapping("/studyAnswer")
	String studyAnswer(Model model,String id){
		model.addAttribute("id",id);
		return "cultivateWeb/study/studyAnswer";
	}

	@Log("随机练习")
	@GetMapping("/studyPractice")
	String studyPractice(Model model,String id,String courseId,String isTestScores){
		model.addAttribute("id",id);
		model.addAttribute("courseId",courseId);
		model.addAttribute("isTestScores",isTestScores);
		return "cultivateWeb/study/studyPractice";
	}

	@Log("试卷测试")
	@GetMapping("/studyExam")
	String studyExam(Model model,String id,String type,String isTestScores){
		model.addAttribute("id",id);
		model.addAttribute("type",type);
		model.addAttribute("isTestScores",isTestScores);
		return "cultivateWeb/study/studyExam";
	}

    @Log("解析详情")
    @GetMapping("/studyAnswerDetail")
    String studyAnswerDetail(Model model,String answerId,String courseId){
        model.addAttribute("answerId",answerId);
        model.addAttribute("courseId",courseId);
        return "cultivateWeb/study/studyAnswerDetail";
    }

    @Log("班级考试")
    @GetMapping("/studyClassesExam")
    String studyClassesExam(Model model,String id ,String type){
        model.addAttribute("id",id);
        model.addAttribute("type",type);
        return "cultivateWeb/study/studyClassesExam";
    }
    @Log("解析详情")
    @GetMapping("/studyClassesExamAnswerDetail")
    String studyClassesExamAnswerDetail(Model model,String paperId){
        model.addAttribute("paperId", paperId);

        return "cultivateWeb/study/studyClassesExamAnswerDetail";
    }


	@Log("章节详情—视频播放")
	@GetMapping("/studyChapter")
	String studyChapter(Model model,String id,String chapterId,String courseId){
			model.addAttribute("chapterId",chapterId);
			model.addAttribute("id",id);
			model.addAttribute("courseId",courseId);
			return "cultivateWeb/study/studyChapter";
	}

}
